package it.cast.wechat.cotroller;

import it.cast.wechat.pojo.Category;
import it.cast.wechat.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("category/addCategory")
    public String addCategory(Category category){
        categoryService.addCategory(category);
        return "redirect:/category/queryAllCategory";
    }

    @RequestMapping("category/modifyCategory")
    public String modifyCategory(Category category){
        categoryService.modifyCategory(category);
        return "redirect:/category/queryAllCategory";
    }

    @RequestMapping("category/deleteCategoryById")
    public String deleteCategoryById(String categoryId){
        categoryService.deleteCategoryById(categoryId);
        return "redirect:/category/queryAllCategory";
    }

    @RequestMapping("category/queryCategoryById")
    public String queryCategoryById(String categoryId, Map<String,Object> map){
        Category category = categoryService.queryCategoryById(categoryId);
        map.put("category",category);
        return "admin/category/edit";
    }

    @RequestMapping("category/queryAllCategory")
    public String queryAllCategory(Map<String,Object> map){
        List<Category> categoryList = categoryService.queryAllCategory();
        map.put("categoryList",categoryList);
        return "admin/category/list";
    }

    //获取分类列表
    @RequestMapping("category/categoryList")
    @ResponseBody
    public List<Category> categoryList(){
        //异步刷新得到结果，然后进行返回
        List<Category> categoryList = categoryService.queryAllCategory();
        return categoryList;
    }
}
